<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Cache;
use think\Db;
use think\Exception;
use think\Request;

/**
 * 商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $model = null;

    public function __construct()
    {
        parent::__construct();
        $this->model = new \app\common\model\Goods;
    }

    /**
     * 获取 商品列表
     *
     */
    public function index()
    {
        $map = [
            'market_switch' => 1
        ];


        //显示10条分页数据
        $data = $this->model
            ->field('id,name,brief,price,costprice,mktprice,image,stock')
            ->where($map)
            ->order('top_switch desc,weigh asc')
            ->paginate(10);

        $data = addWebSiteUrl($data, ['image']);

        foreach($data as $k=>$v){

        }

        $this->success('i18n:62',$data);
    }

    /**
     * 商品列表详情 TODO 简单处理 默认去掉多规格
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $id = $this->request->request('id');
        $map = [];

        $data = $this->model
            ->field('id,name,price,brief,stock,image,images,intro_content,spes_desc,params')
            ->with(['products'=>function($query){
                $query->field('id,goods_id,price,is_defalut,image,sn,stock,spes_desc');
            }])
            ->where($map)
            ->find($id);

        if (!$data){
            $this->error('i18n:172');
        }

        //图片相对地址转绝对地址 begin

        if ($data['image']) {
            $data['image'] = getRealUrl($data['image']);
        }

        if ($data['images']) {
            $images = explode(',',$data['images']);
            foreach ($images as $k =>$v){
                $images[$k] = getRealUrl($v);
            }
            $data['images'] = $images;
        }

        //编辑框图片转绝对地址
        $intro_images = [];
        if (isset($data['intro_content'])) {
            preg_match_all('/(?<=img.src=").*?(?=")/', $data['intro_content'], $out, PREG_PATTERN_ORDER);      //正则匹配img标签的src属性，返回二维数组
            if (!empty($out)) {
                foreach ($out as $v) {
                    foreach ($v as $j) {
                        $url = getRealUrl($j);
                        $intro_images[] = $url;
                        $data['intro_content'] = str_replace($j, $url, $data['intro_content']);   //替换相对路径为绝对路径
                    }
                }
            }
        }
        $data['intro_images'] = $intro_images;
        //图片处理 end

        //多规格参数
        //$data['spes_desc'] = unserialize($data['spes_desc']);
        $params = unserialize($data['params']);
        if (is_array($params)){
            foreach($params as $k=>$v){
                if (is_array($v)){
                    $params[$k] = implode('|',$v);
                }
            }
        }
        $data['params'] = $params;

        $this->success('i18n:62',$data);
    }




}
